import { BrowserWindow, ipcMain } from 'electron';
import { WindowResizer } from './WindowResizer';
import { APIClient } from './APIHandler/APIClient';
import { IPCSender } from './IPCSender';

export class IPCListener {
  private readonly windowResizer: WindowResizer;
  private readonly APIClient: APIClient;
  private readonly browserWindow: BrowserWindow;

  constructor(browserWindow: BrowserWindow, windowResizer: WindowResizer, APIClient: APIClient) {
    this.browserWindow = browserWindow;
    this.windowResizer = windowResizer;
    this.APIClient = APIClient;
  }

  public handleInputSizeChange() {
    // @ts-ignore
    ipcMain.on('input-element-change', (event, increaseBy) => {
      this.windowResizer.increaseWindowSize(increaseBy);
    });
  }

  public handleTranslationRequest() {
    // @ts-ignore
    ipcMain.on('window-on-message', async (event, message: object) => {
      const resultData: string[] | null = await this.APIClient.fetchData(
        message['requiredText'],
        message['_from'],
        message['_to']
      );
      IPCSender.sendMessage(this.browserWindow, resultData);
      this.windowResizer.increaseWindowSize(100);
    });
  }

  public monitorTextEmpty() {
    // @ts-ignore
    ipcMain.on('text-empty', (event) => {
      this.windowResizer.decreaseWindowSize(100);
    });
  }
}
